package com.dangz.dans.service;

import com.dangz.dans.dao.ReplyRepository;
import com.dangz.dans.entity.CommonReqEntity;
import com.dangz.dans.model.req.ReplyPageReq;
import com.dangz.dans.model.wx.Reply;
import com.dangz.dans.model.wx.Setting;
import com.dangz.dans.utils.PageUtil;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dangz
 * @since 2018/2/2 14:31
 */
@Service
public class ReplyServiceImpl {
    @Autowired
    private ReplyRepository replyRepository;

    public Reply save(Reply reply) {
        return replyRepository.save(reply);
    }

    public Reply findByOrdernoAndPid(String pid) {
        return replyRepository.findByOrdernoAndPid(0, pid);
    }

    public List<Reply> findAll(final String pid) {
        Specification<Reply> specification = new Specification<Reply>() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();
                if (StringUtils.isNotEmpty(pid)) {
                    list.add(cb.equal(root.get("pid").as(String.class),  pid ));
                }
                Predicate[] p = new Predicate[list.size()];
                return query.where(list.toArray(p)).getRestriction();
            }
        };
        return replyRepository.findAll(specification, new Sort(Sort.Direction.DESC, "orderno"));
    }

    public PageUtil listPage(CommonReqEntity<ReplyPageReq> req){
        Pageable pageable = new PageRequest(req.getCurPage() - 1, req.getPageSize(),
                new Sort(Sort.Direction.DESC, "orderno"));
        Specification<Reply> specification = new Specification<Reply>() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();
                if (StringUtils.isNotEmpty(req.getRequestEntity().getTitle())) {
                    list.add(cb.like(root.get("title").as(String.class), "%" + req.getRequestEntity().getTitle() + "%"));
                }
                if (StringUtils.isNotEmpty(req.getRequestEntity().getPid())) {
                    list.add(cb.equal(root.get("pid").as(String.class), req.getRequestEntity().getPid()));
                }
                Predicate[] p = new Predicate[list.size()];
                return query.where(list.toArray(p)).getRestriction();
            }
        };
        return new PageUtil ( replyRepository.findAll(specification, pageable) );
    }
}
